package com.yz.lzq.liwushuo.ui.modules.home.boutique;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerClickListener;
import com.yz.lzq.liwushuo.R;
import com.yz.lzq.liwushuo.config.GlideImageLoader;
import com.yz.lzq.liwushuo.customview.NoScrollGridView;
import com.yz.lzq.liwushuo.decoration.SpacesItemDecoration;
import com.yz.lzq.liwushuo.jsonbean.home.boutique.HomeBoutiqueBannerBean;
import com.yz.lzq.liwushuo.jsonbean.home.boutique.HomeBoutiqueFenLeiBean;
import com.yz.lzq.liwushuo.jsonbean.home.boutique.HomeBoutiqueRecycleBean;
import com.yz.lzq.liwushuo.ui.modules.comminterface.OnMyItemClickListener;
import com.yz.lzq.liwushuo.ui.modules.home.boutique.gridview.christmas.ChristmasActivity;
import com.yz.lzq.liwushuo.ui.modules.home.boutique.gridview.creditstopic.CreditsTopicActivity;
import com.yz.lzq.liwushuo.ui.modules.home.nomal.HomeNomalFragmentRecycleViewAdapter;
import com.yz.lzq.liwushuo.ui.webview.home.HomeWebActivity;
import com.yz.lzq.liwushuo.ui.webview.home.HomeWebContentActivity;
import com.yz.lzq.liwushuo.utils.ActivityUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/11/12.
 */

public class HomeBoutiqueFragment extends Fragment implements HomeBoutiqueContract.HomeBoutiqueViewInterface {
    @Bind(R.id.recyvleView_Home_Jingpin)
    XRecyclerView recyvleViewHomeBoutique;
    /* @Bind(R.id.imageView_emptyView)
     ImageView imageViewEmptyView;
     @Bind(R.id.relativeLayout_emptyView)
     RelativeLayout relativeLayoutEmptyView;*/
    private Context mContext = null;
    private LayoutInflater layoutInflater;
    private Banner banner_Boutique_addView;
    private GridView gridView_Boutique_addView;
    private HomeBoutiqueGridViewAdapter adapter;
    private View headerView;
    private HomeBoutiquePresenter presenter = null;
    //recycleView的适配器
    private HomeNomalFragmentRecycleViewAdapter recycleViewAdapter = null;
    //是否刷新
    boolean isRefreshing = false;
    //是否加载
    private boolean isLoadingMore = false;
    List<HomeBoutiqueFenLeiBean.DataBean.SecondaryBannersBean> mList_Secondary_banners = new ArrayList<>();
    List<HomeBoutiqueRecycleBean.DataBean.ItemsBean> mList = new ArrayList<>();
    private List<String> stringList = new ArrayList<>();
    private NoScrollGridView gridView_jingpin_addView;
    private HomeBoutiqueRecycleBean currentRecycleBean;

    /**
     * 初始化Homefragment对象的单例模式
     *
     * @return 当前fragment对象
     */
    public static HomeBoutiqueFragment getInstance() {
        HomeBoutiqueFragment fragment = new HomeBoutiqueFragment();
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    /**
     * @param inflater           布局加载器
     * @param container          父容器
     * @param savedInstanceState 保存状态的信息
     * @return 返回加载布局
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_jingpin, container, false);
        ButterKnife.bind(this, view);
        return view;
    }


    /**
     * 为控件赋值
     *
     * @param savedInstanceState 获取保存状态的信息
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        initAddView();
//        initEmptyView();
        LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        recyvleViewHomeBoutique.setLayoutManager(manager);
        recyvleViewHomeBoutique.addItemDecoration(new SpacesItemDecoration(5));
        recyvleViewHomeBoutique.setHasFixedSize(true);
        presenter = new HomeBoutiquePresenter(mContext, this);
        presenter.showBanner();
        presenter.showFenLei();
        recyvleViewHomeBoutique.addHeaderView(headerView);
        presenter.showRecycleView(false);
        //下拉刷新 和 上拉加载
        recyvleViewHomeBoutique.setLoadingListener(new XRecyclerView.LoadingListener() {
            //下拉刷新
            @Override
            public void onRefresh() {
                isRefreshing = !isRefreshing;
                presenter.showFenLei();
                presenter.showRecycleView(false);
                presenter.showBanner();
            }

            @Override
            public void onLoadMore() {
                isLoadingMore = !isLoadingMore;
                presenter.showRecycleView(true);

            }
        });
        recycleViewAdapter = new HomeNomalFragmentRecycleViewAdapter(mContext, mList, new OnMyItemClickListener() {
            @Override
            public void clickMyItem(View view, int position) {
                if (null != mList.get(position).getRedirect_value()) {
                    ActivityUtil.startWebViewActivity(mContext, HomeWebActivity.class, new String[]{mList.get(position).getRedirect_value()});
                } else if (null != mList.get(position).getUrl()) {
                    ActivityUtil.startWebViewActivity(mContext, HomeWebContentActivity.class, new String[]{mList.get(position).getUrl()});

                }
            }
        });
        recyvleViewHomeBoutique.setAdapter(recycleViewAdapter);
    }


    /**
     * RecyclerView的头视图
     */
    private void initAddView() {
        headerView = layoutInflater.inflate(R.layout.xrcyclerview_headerview, null);
        banner_Boutique_addView = (Banner) headerView.findViewById(R.id.banner_jingpin_addView);
        //设置banner样式
        banner_Boutique_addView.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner_Boutique_addView.setImageLoader(new GlideImageLoader());
        //设置banner动画效果
        banner_Boutique_addView.setBannerAnimation(Transformer.DepthPage);
        //设置自动轮播，默认为true
        banner_Boutique_addView.isAutoPlay(true);
        //设置轮播时间
        banner_Boutique_addView.setDelayTime(3000);
        //设置指示器位置（当banner模式中有指示器时）
        banner_Boutique_addView.setIndicatorGravity(BannerConfig.CENTER);
//        gridView_Boutique_addView = (GridView) headerView.findViewById(R.id.gridView_jingpin_addView);
        gridView_jingpin_addView = (NoScrollGridView) headerView.findViewById(R.id.gridView_jingpin_addView);
        adapter = new HomeBoutiqueGridViewAdapter(mContext, mList_Secondary_banners, new OnMyItemClickListener() {
            @Override
            public void clickMyItem(View view, int position) {
                switch (position) {
                    case 0:

                        break;
                    case 1:
                        ActivityUtil.statActivity(mContext, ChristmasActivity.class, mList_Secondary_banners.get(position).getTarget_url(), "圣诞特辑");
                        break;
                    case 2:
                        ActivityUtil.statActivity(mContext, CreditsTopicActivity.class, mList_Secondary_banners.get(position).getTarget_url(), "");
                        break;
                    case 3:
                        ActivityUtil.statActivity(mContext, ChristmasActivity.class, mList_Secondary_banners.get(position).getTarget_url(), "跟我去巡店");
                        break;
                    case 4:
                        ActivityUtil.statActivity(mContext, ChristmasActivity.class, mList_Secondary_banners.get(position).getTarget_url(), "一周精选");
                        break;
                    case 5:
//                        ActivityUtil.statActivity(mContext, HomeWebContentActivity.class, mList_Secondary_banners.get(position).getTarget_url(), "");
                        break;
                }
            }
        });
//            gridView_Boutique_addView.setAdapter(adapter);
        gridView_jingpin_addView.setAdapter(adapter);
    }


    /**
     * 视图销毁
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    /**
     * 碎片销毁
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 回调获取的bannerBean
     *
     * @param bannerBean
     */
    @Override
    public void getHomeJingPinBannerBean(HomeBoutiqueBannerBean bannerBean) {
//        Log.i("tag", "getHomeBoutiqueBannerBean:=========>>>>>>>> " + bannerBean.getData().getBanners().size());
        if (stringList.size() == 0) {
            for (int i = 0; i < bannerBean.getData().getBanners().size(); i++) {
                stringList.add(bannerBean.getData().getBanners().get(i).getImage_url());
            }

        } else if (isRefreshing) {
            stringList.clear();
            for (int i = 0; i < bannerBean.getData().getBanners().size(); i++) {
                stringList.add(bannerBean.getData().getBanners().get(i).getImage_url());
            }
        }

        //设置图片集合
        banner_Boutique_addView.setImages(stringList);
        //也面跳转
        banner_Boutique_addView.setOnBannerClickListener(new OnBannerClickListener() {
            @Override
            public void OnBannerClick(int position) {
//                bannerBean.getData().getBanners().get(i)
            }
        });
        //banner设置方法全部调用完毕时最后调用
        banner_Boutique_addView.start();

    }

    /**
     * 回调获取的fenLeiBean
     *
     * @param fenLeiBean
     */
    @Override
    public void getHomeBoutiqueFenLeiBean(HomeBoutiqueFenLeiBean fenLeiBean) {
        Log.i("tag", "getHomeBoutiqueFenLeiBean================>>>: " + fenLeiBean.getData().getSecondary_banners().size());
        if (mList_Secondary_banners.size() == 0) {
            mList_Secondary_banners.addAll(fenLeiBean.getData().getSecondary_banners());
            adapter.reLoadGridView(fenLeiBean.getData().getSecondary_banners());
        } else if (isRefreshing) {
            adapter.reLoadGridView(fenLeiBean.getData().getSecondary_banners());
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (null != recycleViewAdapter) {
            recycleViewAdapter.reloadRecyclerView(mList, true);
        }
    }

    /**
     * 回调获取的recycleBean
     *
     * @param recycleBean
     */
    @Override
    public void getHomeBoutiqueRecycleBean(HomeBoutiqueRecycleBean recycleBean) {
        Log.i("tag", "getHomeBoutiqueRecycleBean============>: " + recycleBean.getData().getItems().size());
        currentRecycleBean = recycleBean;
//        initEmptyView();
        if (mList.size() == 0) {
            mList.addAll(recycleBean.getData().getItems());
            recycleViewAdapter.reloadRecyclerView(recycleBean.getData().getItems(), true);

        } else if (isRefreshing) {
            mList.clear();
            mList.addAll(recycleBean.getData().getItems());
            recycleViewAdapter.reloadRecyclerView(mList, true);
            recyvleViewHomeBoutique.refreshComplete();
            isRefreshing = !isRefreshing;
        } else if (isLoadingMore) {
            mList.addAll(recycleBean.getData().getItems());
            recycleViewAdapter.reloadRecyclerView(mList, true);
            recyvleViewHomeBoutique.loadMoreComplete();
            isLoadingMore = !isLoadingMore;
        }
    }

    @Override
    public String getNextUrl() {
        if (null != currentRecycleBean) {
            return currentRecycleBean.getData().getPaging().getNext_url();
        }
        return null;
    }

}
